package com.demo.client;

import android.util.Log;

public class ReconnectionThread extends Thread {

	private static final String TAG = LogUtil.getClassName(ReconnectionThread.class);
	
	private int waiting = 0;
	
	private XmppManager xmppManager = null;
	
	public ReconnectionThread(XmppManager xmppManager)
	{
		this.xmppManager = xmppManager;
		waiting = 0;
	}
	@Override
	public void run() {
		while(!isInterrupted())
		{
			Log.d(TAG, "Trying to reconnect in "+waiting()+" secongd");
			try {
				Thread.sleep((long)waiting*1000L);
				xmppManager.connect();
				waiting++;
			} catch (final InterruptedException e) {
				xmppManager.getHandler().post(new Runnable() {
					@Override
					public void run() {
						xmppManager.getConnectionListener().reconnectionFailed(e);
					}
				});
				e.printStackTrace();
			}
		}
	}
	private int waiting()
	{
		if(waiting>20){
			return 600;
		}
		if(waiting>13){
			return 300;
		}
		return waiting<=7? 10 : 60;
	}

	
}
